<?php

/**
 * crpCasa
 *
 * @copyright (c) 2008-2010 Daniele Conca
 * @link http://code.zikula.org/crpcasa Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpCasa
 */

/**
 * initialise block
 *
 */
function crpCasa_randomalbumsblock_init()
{
	// Security
	pnSecAddSchema('crpCasaAlbumsRandBlock::', 'Block title::');
}

/**
 * get information on block
 *
 */
function crpCasa_randomalbumsblock_info()
{
	return array (
		'text_type' => 'Random Picasa albums',
		'module' => 'crpCasa',
		'text_type_long' => 'Thumbnails of Random Picasa albums',
		'allow_multiple' => true,
		'form_content' => false,
		'form_refresh' => false,
		'show_preview' => true
	);
}

/**
 * display block
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function crpCasa_randomalbumsblock_display($blockinfo)
{
	// security check
	if (!SecurityUtil :: checkPermission('crpCasaAlbumsRandBlock::', "$blockinfo[title]::", ACCESS_READ))
		return;

	if (!pnModAvailable('crpCasa'))
		return;

	// Break out options from our content field
	$vars = pnBlockVarsFromContent($blockinfo['content']);
	// get all module vars for later use
	$modvars = pnModGetVar('crpCasa');

	$albums = array();
	$expAlbums = array();

	$albums = pnModAPIFunc('crpCasa','user','list_albums', array('startnum'=>'1', 'albumsperpage'=> null));
	$tempKey = array_rand($albums,$vars['albumsperpage']);
	shuffle($tempKey);

	foreach ($tempKey as $albumkey)
	{
		$expAlbums[] = $albums[$albumkey];
	}

	// create the output object
	$render = & pnRender :: getInstance('crpCasa', false);

	$render->assign('albums', $expAlbums);
	$render->assign($modvars);
	$render->assign('albumsperpage', $vars['albumsperpage']);
	$render->assign('showcounter', $vars['showcounter']);
	$render->assign('showdate', $vars['showdate']);

	$blockinfo['content'] = $render->fetch('blocks/crpcasa_block_albums_random.htm');

	return pnBlockThemeBlock($blockinfo);
}

/**
 * modify block settings
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function crpCasa_randomalbumsblock_modify($blockinfo)
{
	// Break out options from our content field
	$vars = pnBlockVarsFromContent($blockinfo['content']);

	// Create output object
	$render = & pnRender :: getInstance('crpCasa', false);

	// assign the block vars
	$render->assign($vars);

	// Return the output that has been generated by this function
	return $render->fetch('blocks/crpcasa_block_albums_random_modify.htm');

}

/**
 * update block settings
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function crpCasa_randomalbumsblock_update($blockinfo)
{
	// Get current content
	$vars = pnBlockVarsFromContent($blockinfo['content']);

	// alter the corresponding variable
	$vars['albumsperpage'] = FormUtil :: getPassedValue('albumsperpage', null);
	$vars['showcounter'] = FormUtil :: getPassedValue('showcounter', null);
	$vars['showdate'] = FormUtil :: getPassedValue('showdate', null);

	// write back the new contents
	$blockinfo['content'] = pnBlockVarsToContent($vars);

	// clear the block cache
	$render = & pnRender :: getInstance('crpCasa');
	$render->clear_cache('blocks/crpcasa_block_albums_random.htm');

	return $blockinfo;
}
?>